home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / book / Chap07 / BMPVIEW / bmpview.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-21  |  2.8 KB  |  128 lines

  1. /*
  2.  * OpenGL bitmap viewing demo from Chapter 7.
  3.  *
  4.  * Written by Michael Sweet.
  5.  */
  6.  
  7. /*
  8.  * Include necessary headers.
  9.  */
  10.  
  11. #include "bitmap.h"
  12. #include <GL/glut.h>
  13.  
  14.  
  15. /*
  16.  * Globals...
  17.  */
  18.  
  19. int        Width;       /* Width of window */
  20. int        Height;      /* Height of window */
  21. BITMAPINFO *BitmapInfo; /* Bitmap information */
  22. GLubyte    *BitmapBits; /* Bitmap data */
  23.  
  24.  
  25. /*
  26.  * Functions...
  27.  */
  28.  
  29. void Redraw(void);
  30. void Resize(int width, int height);
  31.  
  32.  
  33. /*
  34.  * 'main()' - Open a window and display a bitmap.
  35.  */
  36.  
  37. int                /* O - Exit status */
  38. main(int  argc,    /* I - Number of command-line arguments */
  39.      char *argv[]) /* I - Command-line arguments */
  40.     {
  41.     glutInit(&argc, argv);
  42.     glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
  43.     glutInitWindowSize(792, 573);
  44.     glutCreateWindow("Bitmap File Viewer");
  45.     glutReshapeFunc(Resize);
  46.     glutDisplayFunc(Redraw);
  47.  
  48.     if (argc > 1)
  49.         BitmapBits = LoadDIBitmap(argv[1], &BitmapInfo);
  50.     else
  51.         BitmapBits = LoadDIBitmap("mountain.bmp", &BitmapInfo);
  52.  
  53.     glutMainLoop();
  54.     if (BitmapInfo)
  55.         {
  56.     free(BitmapInfo);
  57.     free(BitmapBits);
  58.     }
  59.     return (0);
  60.     }
  61.  
  62.  
  63. /*
  64.  * 'Redraw()' - Redraw the window...
  65.  */
  66.  
  67. void
  68. Redraw(void)
  69.     {
  70.     GLfloat xsize, ysize;     /* Size of image */
  71.     GLfloat xoffset, yoffset; /* Offset of image */
  72.     GLfloat xscale, yscale;   /* Scaling of image */
  73.  
  74.     /* Clear the window to black */
  75.     glClearColor(0.0, 0.0, 0.0, 1.0);
  76.     glClear(GL_COLOR_BUFFER_BIT);
  77.  
  78.     if (BitmapInfo)
  79.         {
  80.         xsize = Width;
  81.         ysize = BitmapInfo->bmiHeader.biHeight * xsize /
  82.                 BitmapInfo->bmiHeader.biWidth;
  83.         if (ysize > Height)
  84.             {
  85.             ysize = Height;
  86.             xsize = BitmapInfo->bmiHeader.biWidth * ysize /
  87.                     BitmapInfo->bmiHeader.biHeight;
  88.             }
  89.  
  90.         xscale  = xsize / BitmapInfo->bmiHeader.biWidth;
  91.         yscale  = ysize / BitmapInfo->bmiHeader.biHeight;
  92.  
  93.         xoffset = (Width - xsize) * 0.5;
  94.         yoffset = (Height - ysize) * 0.5;
  95.  
  96.         glRasterPos2f(xoffset, yoffset);
  97.         glPixelZoom(xscale, yscale);
  98.  
  99.         glDrawPixels(BitmapInfo->bmiHeader.biWidth,
  100.                      BitmapInfo->bmiHeader.biHeight,
  101.                      GL_BGR_EXT, GL_UNSIGNED_BYTE, BitmapBits);
  102.         }
  103.  
  104.     glFinish();
  105.     }
  106.  
  107.  
  108. /*
  109.  * 'Resize()' - Resize the window...
  110.  */
  111.  
  112. void
  113. Resize(int width,  /* I - Width of window */
  114.        int height) /* I - Height of window */
  115.     {
  116.     /* Save the new width and height */
  117.     Width  = width;
  118.     Height = height;
  119.  
  120.     /* Reset the viewport... */
  121.     glViewport(0, 0, width, height);
  122.  
  123.     glMatrixMode(GL_PROJECTION);
  124.     glLoadIdentity();
  125.     glOrtho(0.0, (GLfloat)width, 0.0, (GLfloat)height, -1.0, 1.0);
  126.     glMatrixMode(GL_MODELVIEW);
  127.     }
  128.